﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace market
{
    public partial class orderlist : System.Web.UI.Page
    {
        CommonClass ccObj = new CommonClass();
        DBClass dbObj = new DBClass();
        OrderClass ocObj = new OrderClass();
        protected string menu = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            menu = Request.QueryString["menu"];
            if (!IsPostBack)
            {
                check_Login();
                //判断是否已点击“搜索”按钮
                ViewState["search"] = null;
                pageBind(); //绑定订单信息
            }
        }
        public void check_Login()
        {
            if ((Session["AName"] == null))
            {
                Response.Write("<script>alert('对不起！您不是管理员，无权限浏览此页！');location='Login.aspx'</script>");
                Response.End();
            }
        }
        //绑定货品总额
        public string GetVarGF(string strGoodsFee)
        {
            return ccObj.VarStr(strGoodsFee, 2);
        }
        //绑定运费
        public string GetVarSF(string strShipFee)
        {
            return ccObj.VarStr(strShipFee, 2);
        }
        //绑定总金额
        public string GetVarTP(string strTotalPrice)
        {
            return ccObj.VarStr(strTotalPrice, 2);
        }
        public string GetStatus(int IntOrderID)
        {
            string strSql = "select (case IsConfirm when '0' then '未确认' when '1' then '已确认' end ) as IsConfirm";
            strSql += ",(case IsSend when '0' then '未发货' when '1' then '已发货' end ) as IsSend";
            strSql += "  from OrderInfo where OrderID=" + IntOrderID;
            DataTable dsTable = dbObj.GetDataSetStr(strSql, "tbOI");
            return (dsTable.Rows[0][0].ToString() + "|" + dsTable.Rows[0][1].ToString());
        }
        public string GetAdminName(int IntOrderID)
        {
            string strSql = "select AdminName from Admin ";
            strSql += "where AdminID=(select AdminID from OrderInfo";
            strSql += " where OrderID='" + IntOrderID + "')";
            SqlCommand myCmd = dbObj.GetCommandStr(strSql);
            string strAdminName = (dbObj.ExecScalar(myCmd).ToString());
            if (strAdminName == "")
            {
                return "无";
            }
            else
            {
                return strAdminName;
            }
        }
        string strSql;
        public void pageBind()
        {
            strSql = "select * from OrderInfo where ";
            string strOL = Request["OrderList"].Trim();
            string TitleInfo = "";
            switch (strOL)
            {
                case "00":
                    strSql += "IsConfirm=0";
                    TitleInfo = "未确认";
                    break;
                case "01":
                    strSql += "IsConfirm=1";
                    TitleInfo = "已确认";
                    break;
                case "10":
                    strSql += "IsSend=0";
                    TitleInfo = "未发货";
                    break;
                case "11":
                    strSql += "IsSend=1";
                    TitleInfo = "已发货";
                    break;
                default:
                    break;
            }
            strSql += " order by OrderDate Desc";
            this.labTitleInfo.Text = TitleInfo;
            //获取查询信息，并将其绑定到GridView控件中
            DataTable dsTable = dbObj.GetDataSetStr(strSql, "tbOI");
            this.gvOrderList.DataSource = dsTable.DefaultView;
            this.gvOrderList.DataKeyNames = new string[] { "OrderID" };
            this.gvOrderList.DataBind();
        }
        public void gvSearchBind()
        {
            int IntOrderID = 0; //输入订单号
            int IntNF = 0;        //判断是否输入收货人
            string strName = "";  //输入收货人名
            int IntIsConfirm = 0;//是否确认
            int IntIsSend = 0;   //是否发货
            if (this.txtKeyword.Text == "" && this.txtName.Text == "" && this.ddlConfirmed.SelectedIndex == 0 && this.ddlShipped.SelectedIndex == 0)
            {
                pageBind();
            }
            else
            {
                if (this.txtKeyword.Text != "")
                {
                    IntOrderID = Convert.ToInt32(this.txtKeyword.Text.Trim());
                }
                if (this.txtName.Text != "")
                {
                    IntNF = 1;
                    strName = this.txtName.Text.Trim();
                }
                IntIsConfirm = this.ddlConfirmed.SelectedIndex;
                IntIsSend = this.ddlShipped.SelectedIndex;
                DataTable dsTable = ocObj.ExactOrderSearch(IntOrderID, IntNF, strName, IntIsConfirm, IntIsSend);
                this.gvOrderList.DataSource = dsTable.DefaultView;
                this.gvOrderList.DataKeyNames = new string[] { "OrderID" };
                this.gvOrderList.DataBind();
            }
        }

        protected void gvOrderList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvOrderList.PageIndex = e.NewPageIndex;
            if (ViewState["search"] == null)
            {
                pageBind();
            }
            else
            {
                gvSearchBind();//绑定查询后的订单信息

            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState["search"] = 1;
            gvSearchBind();
        }
        protected void gvOrderList_RowDeleting(object sender, GridViewDeleteEventArgs e) //?   
        {
            string strSql = "select * from OrderInfo where IsConfirm=0 and OrderID=" + Convert.ToInt32(gvOrderList.DataKeys[e.RowIndex].Value);
            if (dbObj.GetDataSetStr(strSql, "tbOrderInfo").Rows.Count > 0)
            {
                //删除订单表中的信息
                string strDelSql = "delete from OrderInfo where OrderId=" + Convert.ToInt32(gvOrderList.DataKeys[e.RowIndex].Value);
                SqlCommand myCmd = dbObj.GetCommandStr(strDelSql);
                dbObj.ExecNonQuery(myCmd);
            }
            else
            {
                Response.Write(ccObj.MessageBox("该订单还未完成，无法删除！"));
                return;
            }
            //重新绑定
            if (ViewState["search"] == null)
            {
                pageBind();
            }
            else
            {
                gvSearchBind();
            }
        }
    }
}